using UnityEngine;
using UnityEngine.UI;

public class HypotheticalGoalManager : MonoBehaviour
{
	private static HypotheticalGoalManager instance;

	[SerializeField]
	private Animator GoalCheckmarkAnimator;

	[SerializeField]
	private AudioSource GoalSource;

	[SerializeField]
	private AudioClip GoalCompleteSound;

	[SerializeField]
	private RawImage GoalCheckmark_LogPreview;

	public static HypotheticalGoalManager Instance => instance;

	public void CompleteGoal(HypothesisGoal goal, bool PlaySound = true)
	{
		//IL_012b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0135: Unknown result type (might be due to invalid IL or missing references)
		int requiredTasks = goal.RequiredTasks;
		string text = "HypothesisGoal_" + goal.GoalPlayprefName;
		int num = PlayerPrefs.GetInt(text, 0);
		PlayerPrefs.SetInt(text, requiredTasks);
		Debug.Log((object)$"Goal: {goal.GoalPlayprefName}, Current: {num}, Required: {requiredTasks}");
		if (num >= requiredTasks)
		{
			Debug.Log((object)"Goal already completed.");
			return;
		}
		bool flag = true;
		HypothesisGoal[] hypothesisGoals = goal.ParentHypothesis.HypothesisGoals;
		foreach (HypothesisGoal hypothesisGoal in hypothesisGoals)
		{
			int num2 = PlayerPrefs.GetInt("HypothesisGoal_" + hypothesisGoal.GoalPlayprefName, 0);
			Debug.Log((object)$"Checking goal {hypothesisGoal.GoalPlayprefName}: {num2}/{hypothesisGoal.RequiredTasks}");
			if (num2 < hypothesisGoal.RequiredTasks)
			{
				flag = false;
			}
		}
		if (flag)
		{
			Debug.Log((object)"All goals completed!");
			if ((Object)(object)goal.ParentHypothesis.LogRewardedOnGoalsCompleted != (Object)null)
			{
				CollectibleLog logRewardedOnGoalsCompleted = goal.ParentHypothesis.LogRewardedOnGoalsCompleted;
				PlayerPrefs.SetInt(logRewardedOnGoalsCompleted.LogPlayerPrefName, 1);
				GoalCheckmark_LogPreview.texture = (Texture)(object)logRewardedOnGoalsCompleted.LogPreview.texture;
				((Graphic)GoalCheckmark_LogPreview).rectTransform.sizeDelta = logRewardedOnGoalsCompleted.LogPreviewScale / 5f;
				GoalCheckmarkAnimator.Play("ContextGoal_GoalComplete_NewLog", -1, 0f);
			}
			else
			{
				GoalCheckmarkAnimator.Play("ContextGoal_GoalComplete", -1, 0f);
			}
		}
		else
		{
			Debug.Log((object)"Not all goals completed.");
			GoalCheckmarkAnimator.Play("ContextGoal_GoalComplete", -1, 0f);
		}
		if (PlaySound)
		{
			GoalSource.PlayOneShot(GoalCompleteSound);
		}
	}

	public void IncrementGoal(HypothesisGoal goal, int amount)
	{
		int requiredTasks = goal.RequiredTasks;
		int num = PlayerPrefs.GetInt("HypothesisGoal_" + goal.GoalPlayprefName, 0);
		int num2 = num;
		if (num + amount >= requiredTasks)
		{
			num2 = requiredTasks;
			CompleteGoal(goal);
		}
		else
		{
			num2 = num + amount;
		}
		PlayerPrefs.SetInt("HypothesisGoal_" + goal.GoalPlayprefName, num2);
	}

	public void IncrementGoal_Silent(HypothesisGoal goal, int amount)
	{
		int requiredTasks = goal.RequiredTasks;
		int num = PlayerPrefs.GetInt("HypothesisGoal_" + goal.GoalPlayprefName, 0);
		int num2 = num;
		if (num + amount >= requiredTasks)
		{
			num2 = requiredTasks;
			CompleteGoal(goal, PlaySound: false);
		}
		else
		{
			num2 = num + amount;
		}
		PlayerPrefs.SetInt("HypothesisGoal_" + goal.GoalPlayprefName, num2);
	}

	private void Awake()
	{
		if ((Object)(object)instance != (Object)null && (Object)(object)instance != (Object)(object)this)
		{
			Object.Destroy((Object)(object)((Component)this).gameObject);
		}
		else
		{
			instance = this;
		}
	}
}
